//
//  LSegmentControl.m
//  CarCare
//
//  Created by ileo on 15/3/19.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "LSegmentControl.h"

@interface LSegmentControl()

@property (nonatomic, copy) NSArray *buttons;

@property (nonatomic, copy) void (^Action)(NSInteger index);

@end

@implementation LSegmentControl

-(instancetype)initWithFrame:(CGRect)frame items:(NSArray *)items{
    self = [super initWithFrame:frame];
    if (self) {
        
        NSInteger num = items.count;
        CGFloat width = self.width / num;
        NSMutableArray *tmps = [NSMutableArray arrayWithCapacity:num];
        __weak __typeof(self) wself = self;
        for (int i = 0; i < num; i++) {
            __block UIButton *button = [[[UIButton buttonWithFrame:CGRectMake(width * i, 0, width, self.height) title:items[i] textColor:[UIColor whiteColor] font:FONT_BUTTON click:^{
                [wself clickAction:button];
            }] resetBackgroundColor:[UIColor lightGrayColor]] setupOnView:self];
            button.tag = i;
            button.layer.cornerRadius = 4;
            [tmps addObject:button];
            [button showDebugFrame];
        }
        self.buttons = tmps;
    }
    return self;
}

-(void)clickAction:(UIButton *)button{

    self.selectedIndex = button.tag;
    if (self.Action) {
        self.Action(button.tag);
    }
    
}

-(void)addAction:(void (^)(NSInteger))action{
    self.Action = action;
}

#pragma mark - set

-(void)setFont:(UIFont *)font{
    _font = font;
    for (UIButton *button in self.buttons) {
        button.titleLabel.font = font;
    }
}

-(void)setBorderColor:(UIColor *)borderColor{
    _borderColor = borderColor;
    for (UIView *view in self.buttons) {
        view.layer.borderColor = borderColor.CGColor;
        view.layer.borderWidth = 1;
    }
}

-(void)setGap:(CGFloat)gap{
    _gap = gap;
    NSInteger num = self.buttons.count;
    CGFloat width = (self.width - gap * (num - 1)) / num;
    for (int i = 0; i < num; i++) {
        UIView *view = self.buttons[i];
        view.left = i * (width + gap);
        view.width = width;
    }
}

-(void)setSelectedIndex:(NSInteger)selectedIndex{
    _selectedIndex = selectedIndex;
    for (UIButton *button in self.buttons) {
        if (button.tag == selectedIndex) {
            button.backgroundColor = self.selectedColor;
            [button setTitleColor:self.selectedTextColor forState:UIControlStateNormal];
        }else{
            button.backgroundColor = self.normalColor;
            [button setTitleColor:self.normalTextColor forState:UIControlStateNormal];
        }
    }
}

-(void)setSelectedColor:(UIColor *)selectedColor{
    _selectedColor = selectedColor;
    for (UIView *view in self.buttons) {
        if (view.tag == self.selectedIndex) {
            view.backgroundColor = selectedColor;
        }
    }
}

-(void)setNormalColor:(UIColor *)normalColor{
    _normalColor = normalColor;
    for (UIView *view in self.buttons) {
        if (view.tag != self.selectedIndex) {
            view.backgroundColor = normalColor;
        }
    }
}

-(void)setNormalTextColor:(UIColor *)normalTextColor{
    _normalTextColor = normalTextColor;
    for (UIButton *button in self.buttons) {
        if (button.tag != self.selectedIndex) {
            [button setTitleColor:normalTextColor forState:UIControlStateNormal];
        }
    }
}

-(void)setSelectedTextColor:(UIColor *)selectedTextColor{
    _selectedTextColor = selectedTextColor;
    for (UIButton *button in self.buttons) {
        if (button.tag == self.selectedIndex) {
            [button setTitleColor:selectedTextColor forState:UIControlStateNormal];
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
